﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using ThoughtWorks.QRCode.Codec;

namespace QrApproach
{
    public static class QrCreateFunc
    {
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="content">二维码内容</param>
        /// <returns><see cref="Bitmap"/>位图图片</returns>
        public static Bitmap CreateQrImg(string content)
        {
            //创建二维码生成类  
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            //设置编码模式  
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //设置编码测量度  尺寸
            qrCodeEncoder.QRCodeScale = 10;
            //设置编码版本  
            qrCodeEncoder.QRCodeVersion = 0;
            //设置编码错误纠正  
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            //生成二维码图片  
            Bitmap image = qrCodeEncoder.Encode(content);
            return image;
        }

        /// <summary>
        /// 解析二维码图片
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static string AnalyticalQr(Bitmap bitmap)
        {
            QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
            return qrCodeDecoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(bitmap));
        }
    }
}
